package com.mz.mattress.api.util;

import com.mz.mattress.api.entity.MerchantDetail;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class ReflectUtil {

    public static boolean hasValues(Class<?> clazz, Object obj) {
        return hasValues(clazz, obj, null);
    }

    public static boolean hasValues(Class<?> clazz, Object obj, String... ignoreFields) {

        Field[] fields = clazz.getDeclaredFields();

        Set<String> ignoreFieldSet = new HashSet<>();
        if (ignoreFields != null) {
            ignoreFieldSet.addAll(Arrays.asList(ignoreFields));
        }

        for (Field field : fields) {

            if (field.getModifiers() != 2) {
                continue;
            }

            field.setAccessible(true);  // 设置可访问私有字段

            if (field.getName().equals("id") ||
                    field.getName().equals("serialVersionUID") ||
                    ignoreFieldSet.contains(field.getName())) {
                continue;
            }

            Object value = null;
            try {
                value = field.get(obj);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
            if (value != null) {
                return true;
            }
        }
        return false;

    }

    public static void main(String[] args) {

        MerchantDetail detail = new MerchantDetail();
        detail.setId(3);
//        detail.setShop("dd");

        boolean dd = ReflectUtil.hasValues(MerchantDetail.class, detail);

        System.out.println(dd);

    }

}
